package com.twentyOne.ClothingLand.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.twentyOne.ClothingLand.service.IAccountBookService;
import com.twentyOne.ClothingLand.service.impl.AccountBookService;
import com.twentyOne.ClothingLand.util.JsonUtil;
import com.twentyOne.ClothingLand.vo.AccountBook;

public class AccountBookAction extends ActionSupport {

	private AccountBook accountBook;
	private IAccountBookService accountBookService = new AccountBookService(); 

	public AccountBook getAccountBook() {
		return accountBook;
	}

	public void setAccountBook(AccountBook accountBook) {
		this.accountBook = accountBook;
	}

	public String addAccountBook() throws Exception {

		accountBookService.addAccountBook(accountBook);
		return null;

	}

	public String searchAccountBookByShopId() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		List<AccountBook> list = accountBookService.searchAccountBookByShopId(accountBook.getShopId());
		if (list != null) {
			String accountBooksJson = JsonUtil.objectsToJson(list);
			out.print(accountBooksJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}

		return null;

	}

	public String searchAccountBookByInfo() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		AccountBook accountBook_ = accountBookService.searchAccountBookByInfo(accountBook.getShopId(), accountBook.getType(), accountBook.getCurrency());
		if (accountBook_ != null) {
			String accountBookJson = JsonUtil.objectToJson(accountBook_);
			out.print(accountBookJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}

		return null;

	}
	
	public String income() throws Exception {

//		accountBookService.income(accountBook, amount);
		return null;

	}
	
	public String expend() throws Exception {

//		accountBookService.expend(accountBook, amount);
		return null;

	}

}
